<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\DeptsAdmin;
use think\exception\ValidateException;

class Depts extends BaseController
{
    /**
     * 部门列表
     */
  public function deptsList()
  {
      try {            
        $data = DeptsAdmin::deptsList();
      } catch (\Exception $e)
      {
        return exceptionJson($e->getMessage(), $e->getCode());
      }

      return successJson($data);
  }

  /**
   * 部门添加
   */
  public function deptAdd() {
    $param = $this->request->post([
      'leader',
      'name',
      'remark'
    ]);

    try {
      $this->validate($param, 'app\common\validate\Depts.sceneaddsave');
      $data = DeptsAdmin::deptAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson($e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /**
   * 部门编辑
   */
  public function deptEdit() {
    $param = $this->request->post([
      'id',
      'leader',
      'name',
      'remark'
    ]);

    try {
      $this->validate($param, 'app\common\validate\Depts.sceneaddsave');
      $data = DeptsAdmin::deptAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson($e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /**
   * 部门删除
   */
  public function deptDelete($id) {
    $param = [
      'id' => $id
    ];

    try {
        $data = DeptsAdmin::deptDelete($param);
    } catch (\Exception $e)
    {
        return exceptionJson($e->getMessage(), $e->getCode());
    }
    
    return successJson($data);
  }
}